package com.huaxin.hxcloud.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.huaxin.common.Entity.RepairTaskDetailDTO;
import com.huaxin.hxcloud.Adapter.HxSpttroCauseDlogAdapter;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.eventBus.HxSpttroCauseEventBus;
import com.huaxindata.common.base.HxBaseDialogFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * @author songxf
 * @date 2018/7/17
 * @Desc 故障原因
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSpstroCauseDlogFragment extends HxBaseDialogFragment {
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    private HxSpttroCauseDlogAdapter mSpTtroCauseDlogAdapter;


    private static List<RepairTaskDetailDTO.Data.malfunctionReason> mTroCauseDTO;
    private static int                                              mWorkTaskState;

    public static HxSpstroCauseDlogFragment newInstance(List<RepairTaskDetailDTO.Data.malfunctionReason> troCauseDTO, int workTaskState) {
        Bundle args = new Bundle();

        HxSpstroCauseDlogFragment fragment = new HxSpstroCauseDlogFragment();
        mTroCauseDTO = troCauseDTO;
        mWorkTaskState = workTaskState;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.windowAnimations = R.style.DialogBottom;
        DisplayMetrics dm = new DisplayMetrics();
        if (mSpTtroCauseDlogAdapter != null && mSpTtroCauseDlogAdapter.getData().size() <= 5) {
            setWidthOnHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            //dialog最高高度不超过屏幕的3/4
            setWidthOnHeight(ViewGroup.LayoutParams.MATCH_PARENT, dm.heightPixels * 3 / 4);
        }

        setWidthOnHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public int setRootViewId() {
        return R.layout.dialog_trouble_cause;
    }

    @Override
    public void initData() {
        initRecyclerView();
    }

    private void initRecyclerView() {
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSpTtroCauseDlogAdapter = new HxSpttroCauseDlogAdapter(getActivity(),mWorkTaskState);
        mRecyclerview.setAdapter(mSpTtroCauseDlogAdapter);
        mSpTtroCauseDlogAdapter.addData(mTroCauseDTO);
    }


    @Override
    public int showPosition() {
        return Gravity.BOTTOM;
    }


    @OnClick({R.id.finish_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_btn:
                dismiss();
                break;
            default:
                break;
        }
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        selectData();

    }

    @Override
    public void dismissAllowingStateLoss() {
        super.dismissAllowingStateLoss();
    }

    /**
     * 选择数据使用EventBus
     */
    private void selectData() {
        EventBus.getDefault().post(new HxSpttroCauseEventBus(mSpTtroCauseDlogAdapter.getData()));
    }
}
